package br.ufc.ck0017.ir.translate;

import br.ufc.ck0017.ir.tree.IR;
import br.ufc.ck0017.ir.tree.IRNode;
import br.ufc.ck0017.temp.Label;
import br.ufc.ck0017.temp.Temp;

public abstract class TranslateCx extends TranslatedExpression {
    @Override
    public IRNode unEx() {
        Temp register = new Temp();
        Label trueLabel = new Label();
        Label falseLabel = new Label();

        return IR.ESEQ(IR.SEQ(IR.MOVE(IR.TEMP(register),
                                      IR.CONST(1)),
                              IR.SEQ(unCx(trueLabel, falseLabel),
                                     IR.SEQ(IR.LABEL(falseLabel),
                                            IR.SEQ(IR.MOVE(IR.TEMP(register),
                                                           IR.CONST(0)),
                                                   IR.LABEL(trueLabel))))),
                       IR.TEMP(register));

    }

    @Override
    public IRNode unNx() {
        Label trueLabel = new Label();
        Label falseLabel = new Label();

        return IR.SEQ(this.unCx(trueLabel, falseLabel), IR.SEQ(IR.LABEL(trueLabel), IR.LABEL(falseLabel)));
    }
}
